package com.qidian.buy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qidian.buy.mapper.ItemSpecificationsEntryMapper;
import com.qidian.buy.pojo.ItemSpecifications;
import com.qidian.buy.pojo.ItemSpecificationsEntry;
import com.qidian.buy.service.ItemSpecificationsEntryService;
import com.qidian.buy.util.QdConstant;
import com.qidian.buy.util.pojo.QdResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author 戴着假发的程序员
 * @company 起点编程
 * @date 2023/9/8 11:04
 */
@Service
public class ItemSpecificationsEntryServiceImpl implements ItemSpecificationsEntryService {
    @Autowired
    private ItemSpecificationsEntryMapper entryMapper;
    @Override
    public QdResult queryByPage(Long speId, int pageNum, int limit) {
        IPage<ItemSpecificationsEntry> page = new Page<>(pageNum,limit);
        QueryWrapper<ItemSpecificationsEntry> wrapper = new QueryWrapper<>();
        wrapper.ne("status", QdConstant.DATA_DELETED);
        wrapper.eq("spe_id",speId);
        return QdResult.ok(entryMapper.selectPage(page,wrapper));
    }

    @Override
    public QdResult save(ItemSpecificationsEntry entry) {
        // 查重
        QueryWrapper<ItemSpecificationsEntry> wrapper = new QueryWrapper<>();
        wrapper.ne("status", QdConstant.DATA_DELETED);
        wrapper.eq("spe_id",entry.getSpeId());
        wrapper.eq("entry_text",entry.getEntryText());
        if(entryMapper.selectOne(wrapper) != null){
            return QdResult.duplicate("规格条目的文本重复");
        }
        entry.setModifyTime(new Date());
        entry.setCreateTime(new Date());
        return QdResult.isOk(entryMapper.insert(entry));
    }
}
